<template>
  <view>
    <!--变更企业法人身份-->
    <uni-popup ref="modifyCompanyLegal">
      <uni-popup-dialog
          type="info"
          cancelText="取消"
          confirmText="确认"
          :title="title"
          @close="cancelModifyCompanyLegal"
          @confirm="updateLegalSubmitForm"
          before-close>
        <template v-slot="content">
          <view class="tip">
            <i class="iconfont colorIcon" style="margin-right: 10rpx;font-size: 26rpx;">&#xe70f;</i>
            请输入法定代表人信息，为确保业务合规，请根据要求完成操作。
          </view>
          <uni-forms
              ref="form"
              :label-width="70" type="line" label-align="right"
              class="paddingAll">
            <view style="padding:15rpx 0;margin-bottom: 10rpx;">
              <span style="font-weight: bold;">法定代表人信息</span>
              <span style="float:right;">
              <uni-tag type="primary" text="选择法定代表人" @click="toSelectStaff"/>
            </span>
            </view>
            <uni-forms-item label="企业名称" name="companyName">
              <uni-easyinput type="text" v-model="companyInfo.name" disabled/>
            </uni-forms-item>
            <uni-forms-item label="法定代表人" name="name">
              <uni-easyinput placeholder="从企业内部组织架构选择法定代表人" type="text" v-model="selectStaff.name" disabled/>
            </uni-forms-item>
            <uni-forms-item label="身份证号" name="idNumber">
              <uni-easyinput v-model="selectStaff.idNumber" type="text" placeholder="选择法定代表人后系统自动填写" disabled/>
            </uni-forms-item>
            <uni-forms-item label="手机号码" name="mobile">
              <uni-easyinput v-model="selectStaff.mobile" type="text" placeholder="选择法定代表人后系统自动填写" disabled/>
            </uni-forms-item>
          </uni-forms>
          <view class="show-text">
            <i class="iconfont colorIcon" style="font-size: 26rpx;margin-right: 10rpx;">&#xe70f;</i>
            <span>提醒：点击“提交”按钮，将进入新法定代表人身份认证操作流程，请知晓以下信息：</span>
            <p style="margin-top: 10rpx;">1、新法定代表人信息核验认证，该操作必须由新法定代表人本人完成。</p>
            <p>2、新法定代表人信息变更后，原法定代表人电子签约账户所有权限将自动失效。</p>
          </view>
        </template>
      </uni-popup-dialog>
    </uni-popup>
    <!--待签署授权协议-->
    <uni-popup ref="modifyCompanyLegalRecord">
      <uni-popup-dialog
          type="info"
          cancelText="撤销转授"
          confirmText="重新发送"
          title="待法定代表人身份认证"
          @close="revodeCompanyLegalChange"
          @confirm="resendNotice"
          before-close>
        <template v-slot="content">
          <uni-icons style="float: right;margin: -80rpx 10rpx 0 0;" type="closeempty" size="18"
                     @click="cancelModifyCompanyLegalRecord"/>
          <view class="show-text" style="text-align: center;">
            <i class="iconfont themeColor" style="font-size:100rpx">&#xe66a;</i>
            <p style="font-weight: 550;">待单位新法定代表人身份认证，请耐心等待！</p>
            <view>
              <p>法定代表人：{{ companyLegalChangeRecord.legalNewNameHide }} </p>
              <p>实名手机号：{{ companyLegalChangeRecord.legalNewMobileHide }}</p>
              <p>身份证号码：{{ companyLegalChangeRecord.legalNewIdNumberHide }}</p>
            </view>
            <p style="color: #999999">
              若新法定代表人未收到系统发送的认证短信，请复制认证链接， 通过微信、钉钉等方式发送，待其人脸核验通过后，新法定代表人变更完成。
            </p>
            <p class="themeColor"
               style="text-align: center; font-size: 28rpx;font-weight: 550;"
               @click="copyLegalSignUrl"
            >点击复制授权链接</p>
          </view>
        </template>
      </uni-popup-dialog>
    </uni-popup>

    <!-- 人员选择 -->
    <select-staff ref="selectStaff" @success="toSetSelectStaff"></select-staff>
  </view>
</template>

<script>
import {
  editLegalPerson,
  getLegalChangeRecordWaitingInfo,
  resendCompanyLegalChangeRecordNotice,
  revodeCompanyLegalChange
} from "@/api/ec/console/index";
import selectStaff from "./selectStaff";

export default {
  components: {selectStaff},
  props: {},
  data() {
    return {
      // 企业信息
      companyInfo: this.$store.getters.companyInfo,
      // 0 变更法定代表人账户信息 ， 1 创建法定代表人账户信息
      type: null,
      // 标题
      title: "",
      // 已选人员数据
      selectStaff: {},
      // 企业法人变更记录
      companyLegalChangeRecord: {},
    };
  },
  methods: {
    /** 打开弹出框 */
    toOpenDialog(type) {
      // 0 变更法定代表人账户信息 ， 1 创建法定代表人账户信息
      this.type = type;
      if (this.type == 1) {
        this.title = "创建法定代表人账户信息";
      } else {
        this.title = "变更法定代表人";
      }
      this.selectStaff = {};
      this.companyLegalChangeRecord = {};
      this.getWaitingInfo();
    },
    /** 转授企业法人取消按钮 */
    cancelModifyCompanyLegal() {
      this.$refs.modifyCompanyLegal.close()
    },
    /** 待签署授权协议关闭按钮 */
    cancelModifyCompanyLegalRecord() {
      this.$refs.modifyCompanyLegalRecord.close()
    },
    // =================================================================================================================
    // ==== 选择授权对象
    // =================================================================================================================
    /** 选择授权对象 */
    toSelectStaff() {
      this.$refs.selectStaff.toOpenDialog(1, this.selectStaff);
    },
    /** 设置已选则授权对象 */
    toSetSelectStaff(selectStaff) {
      this.selectStaff = selectStaff;
    },
    /** 变更企业信息,确定按钮 */
    updateLegalSubmitForm() {
      if (this.selectStaff.id == null || this.selectStaff.id == 0) {
        this.$modal.msgError("请先选择转授对象！");
        return;
      }
      const param = {
        legalNewId: this.selectStaff.id,
        legalNewName: this.selectStaff.name,
        legalNewMobile: this.selectStaff.mobile,
        legalNewIdNumber: this.selectStaff.idNumber
      };
      uni.showLoading({title: '加载中...', mask: true});
      editLegalPerson(param).then(response => {
        uni.hideLoading();
        this.cancelModifyCompanyLegal();
        setTimeout(() => {
          this.getWaitingInfo();
        }, 200);
      }).catch(() => {
        uni.hideLoading();
      });
    },
    /** 获取最新的法人信息变更数据 */
    getWaitingInfo() {
      // 获取管理员变更记录
      uni.showLoading({title: '加载中...', mask: true});
      getLegalChangeRecordWaitingInfo().then((response) => {
        uni.hideLoading();
        if (typeof (response.data) == "undefined") {
          this.$refs.modifyCompanyLegal.open('center')
        } else {
          // 有待签署的变更记录，打开流程记录显示窗口
          this.companyLegalChangeRecord = response.data;
          this.$refs.modifyCompanyLegalRecord.open('center');
        }
      });
    },
    /** 撤销企业法人变更 */
    revodeCompanyLegalChange() {
      // 获取管理员变更记录
      uni.showLoading({title: '加载中...', mask: true});
      revodeCompanyLegalChange().then((response) => {
        this.$modal.msgSuccess("撤销成功！");
        uni.hideLoading();
        this.cancelModifyCompanyLegalRecord()
      }).catch(() => {
        uni.hideLoading();
      });
    },
    /** 重新发送企业法人变更短信 */
    resendNotice() {
      uni.showLoading({title: '加载中...', mask: true});
      resendCompanyLegalChangeRecordNotice().then((response) => {
        uni.hideLoading();
        this.$modal.msgSuccess("发送成功！");
      }).catch(() => {
        uni.hideLoading();
      });
    },
    /** 复制授权链接 */
    copyLegalSignUrl() {
      this.$copyText("尊敬的" + this.companyLegalChangeRecord.legalNewName + "，为避免贵司业务受到影响，请您尽快完成《" + this.companyInfo.name + "》的法定代表人身份认证，点击认证 https://t.lksign.cn/r/" + this.companyLegalChangeRecord.shortUrl).then(
          () => {
            this.$modal.msgSuccess("授权链接已复制到剪切板，可粘贴。");
          },
          () => {
            this.$modal.msgError("复制失败！");
          }
      );
    },
  }
};
</script>
<style scoped lang="scss">

::v-deep .uni-popup-dialog {
  width: 90%;
}

::v-deep .uni-dialog-content {
  padding: 20rpx;
  display: block;
}

.tip {
  background: #E6F6EF;
  padding: 15rpx;
  font-size: 22rpx;
  font-weight: 400;
  color: #555b65;
  line-height: 35rpx;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: baseline;
  -ms-flex-align: baseline;
  align-items: baseline;
  margin-bottom: 25rpx;
}

.show-text {
  color: #666;
  padding: 15rpx;

  p {
    text-align: left;
    font-size: 26rpx;
    text-indent: 1rem;
    margin-top: 25rpx;
  }
}

</style>
